﻿/*
 * @file: NewsDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for NewsDA
/// </summary>
namespace DAL
{
    public class NewsDA : BaseDAL, INews
    {
        private const string tableName = "News";
        private const string newsID = "NewsID";
        private const string title = "Title";
        private const string content = "Content";
        private const string postedDate = "PostedDate";
        private const string isEnable = "IsEnable";

        public NewsDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static int TitleExisted(string _title) {
            try
            {
                return RecordExisted(tableName, title, _title);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region INews Members

        /*
         * @method: GetNews
         * @description: Get all News from News Table
         * @input: allowFilter, TRUE enables ignore hidden records
         * @output: all records corresponding
         */
        public News[] GetNews(bool allowFilter)
        {
            News[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = 1 ORDER BY {2} DESC",
                        tableName, isEnable, postedDate);
                else
                    cmd.CommandText = String.Format("SELECT * FROM {0} ORDER BY {1} DESC", tableName, postedDate);

                String[] columnNames = {    newsID,
                                            title,
                                            content,
                                            postedDate,
                                            isEnable};
                result = SelectCollection<News>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetNewsByNewsID
         * @description: Get News record by NewsID
         * @input: NewsID, allowFilter <TRUE enables ignore hidden records>
         * @output: record corresponding
         */
        public News GetNewsByNewsID(int _newsID, bool allowFilter)
        {
            News[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, newsID, newsID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", newsID), SqlDbType.Int).Value = _newsID;
                }
                else
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, newsID, newsID);
                    cmd.Parameters.Add(String.Format("@{0}", newsID), SqlDbType.Int).Value = _newsID;
                }

                String[] columnNames = {    newsID,
                                            title,
                                            content,
                                            postedDate,
                                            isEnable};
                result = SelectCollection<News>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        /*
         * @method: DisableNews
         * @description: hidden a News
         * @input: NewsID, isEnable <FALSE if hidden>
         * @output: 1 if successful, 0 if error occurred
         */
        public int DisableNews(int _newsID, bool _isEnable)
        {
            int result = 0;
            try
            {
                String[] columnNames = { isEnable };
                Object[] values = { _isEnable };
                String[] keyColumnNames = { newsID };
                Object[] keyColumnValues = { _newsID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: InsertNews
         * @description: insert new recort to News table
         * @input: News entity
         * @output: 1 if successful, 0 if error occurred
         */
        public int InsertNews(News _news, out int autoNumber)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    title,
                                            content};

                Object[] values = { _news.Title,
                                    _news.Content};
                
                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: UpdateNews
         * @description: update News information
         * @input: News entity
         * @output: 1 if successful, 0 if error occurred
         */
        public int UpdateNews(News _news)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    title,
                                            content,
                                            postedDate};
                Object[] values = { _news.Title,
                                    _news.Content,
                                    _news.PostedDate};
                String[] keyColumnNames = { newsID };
                Object[] keyColumnValues = { _news.NewsID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}
